package com.mmy.controller.hotel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.hotel.THotelBase;
import com.mmy.domain.hotel.THotelRoom;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.hotel.THotelBaseServiceImpl;
import com.mmy.service.impl.hotel.THotelRoomServiceImpl;

@Controller
public class THotelRoomController {
	@Autowired
	THotelRoomServiceImpl tHotelRoomServiceImpl;
	@Autowired
	THotelBaseServiceImpl tHotelBaseServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明：跳转创建酒店房间页面
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.28
	 */
	@RequestMapping(value = "/tocreatehotelroom")
	public String ToCreateHotelRoom(HttpServletRequest request, Model mHotelRoom) {
		String strHotelID = request.getParameter("iHotelID");
		THotelBase tHotelBase = tHotelBaseServiceImpl.findByiHotelID(Integer.parseInt(strHotelID)).get(0);
		mHotelRoom.addAttribute("newTHotelBase", tHotelBase);
		return "webbackground/hotelmanagement/createhotelroom";

	}

	/* 提交创建的酒店房间信息 */
	@RequestMapping(value = "/savecreatehotelroom")
	public String saveCreateHotelRoom(THotelRoom tHotelRoom, HttpSession session) {
		// 获得最大房间编号
		tHotelRoom.setRoomID(common.createBaseCode(UtilConstants.TableName.T_HOTEL_ROOM));

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		tHotelRoom.setCreateDate(new Date());

		tHotelRoom.setCreator(tSysUser.getUserName());
		tHotelRoomServiceImpl.save(tHotelRoom.getRoomID());
		tHotelRoomServiceImpl.save(tHotelRoom);
		return "redirect:hotelbaseinfo?iHotelID=" + tHotelRoom.getHotelID();

	}

	/**
	 * 函数功能说明： 修改酒店信息
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	/* 点击修改按钮进入修改页面 */
	@RequestMapping("/toupdateroom")
	public String updateHotelRoomCheckedFind(HttpServletRequest request, Model mRoom) {
		String iRoomID = request.getParameter("iRoomID");
		THotelRoom db_THotelRoom = tHotelRoomServiceImpl.findByiRoomID(Integer.parseInt(iRoomID));
		mRoom.addAttribute("newTHotelRoom", db_THotelRoom);
		THotelBase tHotelBase = tHotelBaseServiceImpl.findByiHotelID(db_THotelRoom.getHotelID()).get(0);
		mRoom.addAttribute("newTHotelBase", tHotelBase);
		return "webbackground/hotelmanagement/updatehotelroom";
	}

	/* 提交修改的酒店信息 */
	@RequestMapping(value = "/saveupdatehotelroom")
	public String updateHotelRoom(THotelRoom tHotelRoom, HttpSession session) throws Exception {
		int iRoomID = tHotelRoom.getRoomID();
		/* 定义一个新的实体类对象 */
		THotelRoom dtHotelRoom = tHotelRoomServiceImpl.findByiRoomID(iRoomID);
		dtHotelRoom.setRoomName(tHotelRoom.getRoomName());
		dtHotelRoom.setRoomType(tHotelRoom.getRoomType());
		dtHotelRoom.setRoomSize(tHotelRoom.getRoomSize());
		dtHotelRoom.setOnFloor(tHotelRoom.getOnFloor());
		dtHotelRoom.setNumber(tHotelRoom.getNumber());
		dtHotelRoom.setWindows(tHotelRoom.getWindows());
		dtHotelRoom.setBreakfast(tHotelRoom.getBreakfast());
		dtHotelRoom.setWifi(tHotelRoom.getWifi());
		dtHotelRoom.setNetwork(tHotelRoom.getNetwork());
		dtHotelRoom.setImgPath(tHotelRoom.getImgPath());
		dtHotelRoom.setMemo(tHotelRoom.getMemo());
		dtHotelRoom.setModifyDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		dtHotelRoom.setReviser(tSysUser.getUserName());

		tHotelRoomServiceImpl.save(dtHotelRoom);

		return "redirect:hotelbaseinfo?iHotelID=" + dtHotelRoom.getHotelID();

	}

	/**
	 * 函数功能说明： 删除酒店房间信息
	 *
	 * @param 参数说明 tHotelRoom
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	@PostMapping("/deleteroomchecked")
	@ResponseBody
	public String deleteHotelRoomChecked(String checkList) {
		String[] strRoomIDs = checkList.split(",");
		List<Integer> iRoomIDs = new ArrayList<>();

		for (String strRoomID : strRoomIDs) {
			iRoomIDs.add(Integer.parseInt(strRoomID));
		}
		THotelRoom dtHotelRoom = tHotelRoomServiceImpl.findByiRoomID(iRoomIDs.get(0));
		tHotelRoomServiceImpl.deleteChecked(iRoomIDs);
		return "redirect:hotelbaseinfo?iHotelID=" + dtHotelRoom.getHotelID();

	}
}
